import UAParser from './ua-parser';

const userAgent = navigator.userAgent;

//仅区分ios和android
const regIphone = /\b(iphone|ipad)\b/i;

export const isIos = () => {
    return regIphone.test(userAgent);
};

const regAndroid = /\bandroid\b/i;
export const isAndroid = () => {
    return regAndroid.test(userAgent);
};

const {OS:{NAME,VERSION},DEVICE:{MODEL,VENDOR,TYPE,MOBILE,TABLET}} = UAParser;
const regs = {
    browser: [
        [
            /((?:[\s\/])uc?\s?browser|(?:juc.+)ucweb)[\/\s]?([\w\.]+)/i
        ],[[NAME, 'UCBrowser'], VERSION],
        [
            /(micromessenger)\/([\w\.]+)/i                                      // WeChat
        ], [[NAME, 'WeChat'], VERSION],
        [
            /(qqbrowserlite)\/([\w\.]+)/i                                       // QQBrowserLite
        ], [NAME, VERSION], [
            /(QQ)\/([\d\.]+)/i                                                  // QQ, aka ShouQ
        ], [NAME, VERSION], [
            /m?(qqbrowser)[\/\s]?([\w\.]+)/i                                    // QQBrowser
        ], [NAME, VERSION], [
            /(BIDUBrowser)[\/\s]?([\w\.]+)/i                                    // Baidu Browser
        ], [NAME, VERSION], [
            /(2345Explorer)[\/\s]?([\w\.]+)/i                                   // 2345 Browser
        ], [NAME, VERSION], [
            /(MetaSr)[\/\s]?([\w\.]+)/i                                         // SouGouBrowser
        ], [NAME], [
            /(LBBROWSER)/i                                      // LieBao Browser
        ], [NAME], [
            /xiaomi\/miuibrowser\/([\w\.]+)/i                                   // MIUI Browser
        ], [VERSION, [NAME, 'MIUI Browser']],
        [
            /\swv\).+(chrome)\/([\w\.]+)/i                                      // Chrome WebView
        ], [[NAME, /(.+)/, '$1 WebView'], VERSION], [
            /((?:oculus|samsung)browser)\/([\w\.]+)/i
        ], [[NAME, /(.+(?:g|us))(.+)/, '$1 $2'], VERSION], [                // Oculus / Samsung Browser
            /android.+version\/([\w\.]+)\s+(?:mobile\s?safari|safari)*/i        // Android Browser
        ], [VERSION, [NAME, 'Android Browser']], [
            /((?:android.+)crmo|crios)\/([\w\.]+)/i                             // Chrome for Android/iOS
        ], [[NAME, 'Chrome'], VERSION],
        [
            /version\/([\w\.]+).+?mobile\/\w+\s(safari)/i                       // Mobile Safari
        ], [VERSION, [NAME, 'Mobile Safari']], [
            /version\/([\w\.]+).+?(mobile\s?safari|safari)/i                    // Safari & Safari Mobile
        ], [VERSION, NAME],
    ],
    device:[
        [
            /\((ip[honed|\s\w*]+);.+(apple)/i                                   // iPod/iPhone
        ], [MODEL, VENDOR, [TYPE, MOBILE]], [
            /\((ip[honed|\s\w*]+);/i                                            // iPod/iPhone
        ], [MODEL, [VENDOR, 'Apple'], [TYPE, MOBILE]],
        [
            /d\/huawei([\w\s-]+)[;\)]/i,
            /(nexus\s6p)/i                                                      // Huawei
        ], [MODEL, [VENDOR, 'Huawei'], [TYPE, MOBILE]],
        [
            /android.+((sch-i[89]0\d|shw-m380s|gt-p\d{4}|gt-n\d+|sgh-t8[56]9|nexus 10))/i,
            /((SM-T\w+))/i
        ], [[VENDOR, 'Samsung'], MODEL, [TYPE, TABLET]],
        [
            /android.+;\s(oppo)\s?([\w\s]+)\sbuild/i                            // OPPO
        ], [VENDOR, MODEL, [TYPE, MOBILE]],
        [
            /android.+;\s(\w+)\s+build\/hm\1/i,                                 // Xiaomi Hongmi 'numeric' models
            /android.+(hm[\s\-_]*note?[\s_]*(?:\d\w)?)\s+build/i,               // Xiaomi Hongmi
            /android.+(mi[\s\-_]*(?:one|one[\s_]plus|note lte)?[\s_]*(?:\d?\w?)[\s_]*(?:plus)?)\s+build/i,    // Xiaomi Mi
            /android.+(redmi[\s\-_]*(?:note)?(?:[\s_]*[\w\s]+))\s+build/i       // Redmi Phones
        ], [[MODEL, /_/g, ' '], [VENDOR, 'Xiaomi'], [TYPE, MOBILE]], [
            /android.+(mi[\s\-_]*(?:pad)(?:[\s_]*[\w\s]+))\s+build/i            // Mi Pad tablets
        ],[[MODEL, /_/g, ' '], [VENDOR, 'Xiaomi'], [TYPE, TABLET]], [
            /android.+;\s(m[1-5]\snote)\sbuild/i                                // Meizu Tablet
        ], [MODEL, [VENDOR, 'Meizu'], [TYPE, TABLET]], [
            /android.+a000(1)\s+build/i,                                        // OnePlus
            /android.+oneplus\s(a\d{4})\s+build/i
        ], [MODEL, [VENDOR, 'OnePlus'], [TYPE, MOBILE]],
        [
            /(android[\w\.\s\-]{0,9});.+build/i                                 // Generic Android Device
        ], [MODEL, [VENDOR, 'Generic']]
    ],
    os:[
        [
            /ip[honead]{2,4}(?:.*os\s([\w]+)\slike\smac|;\sopera)/i             // iOS
        ], [[VERSION, /_/g, '.'], [NAME, 'ios']],
        [
            /android[\/\s-]?([\w\.]*)/i,
        ], [[NAME,'android'], VERSION],
    ]
};

export const ua = new UAParser(userAgent, regs);